package com.java.huxb.backend.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.java.huxb.backend.user.dao.entity.MoocBackendUserT;
import com.java.huxb.backend.user.dao.mapper.MoocBackendUserTMapper;
import com.java.huxb.backend.user.service.UserServiceInterface;
import com.java.huxb.backend.utils.exception.CommonServiceException;
import com.java.huxb.backend.utils.util.MD5Util;
import org.checkerframework.checker.units.qual.C;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author huxb
 * @date 2020/1/28 0028 下午 6:16
 * 用户接口实现类
 */
@Service
public class UserServiceImpl implements UserServiceInterface {

    @Resource
    private MoocBackendUserTMapper moocBackendUserTMapper;

    @Override
    public String findUserByNameAndPwd(String username, String password) throws CommonServiceException {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_name", username);

        MoocBackendUserT user = moocBackendUserTMapper.selectOne(queryWrapper);

        if (user != null) {
            String md5Password = MD5Util.encrypt(password);
            if (md5Password.equals(user.getUserPwd())) {
                return user.getUuid() + "";
            } else {
                throw new CommonServiceException(400, "输入密码错误");
            }
        } else {
            throw new CommonServiceException(400, "该用户不存在！");
        }
    }
}
